﻿using System;
using Assets.Scripts.GtsScripts;
using Sizebox.CharacterEditor;
using UnityEngine;

// Token: 0x020001BE RID: 446
public class AnimationManager : MonoBehaviour
{
	// Token: 0x1700013F RID: 319
	// (get) Token: 0x06000906 RID: 2310 RVA: 0x00054C42 File Offset: 0x00052E42
	// (set) Token: 0x06000907 RID: 2311 RVA: 0x00054C4A File Offset: 0x00052E4A
	public string nameAnimation { get; private set; }

	// Token: 0x06000908 RID: 2312 RVA: 0x00054C54 File Offset: 0x00052E54
	private void Awake()
	{
		this.agent = base.GetComponent<Humanoid>();
		this.unityAnimator = base.gameObject.GetComponent<Animator>();
		this.animationSpeedHash = Animator.StringToHash("animationSpeed");
		this.forwardHash = Animator.StringToHash("forward");
		this.rightHash = Animator.StringToHash("right");
		AnimationManager.iomanager = IOManager.Instance;
		this.giantess = base.GetComponent<Giantess>();
	}

	// Token: 0x06000909 RID: 2313 RVA: 0x00054CC4 File Offset: 0x00052EC4
	public static bool AnimationExists(string animation)
	{
		return AnimationManager.iomanager.animationControllers.ContainsKey(animation);
	}

	// Token: 0x0600090A RID: 2314 RVA: 0x00054CD8 File Offset: 0x00052ED8
	public static void SetAnimatorController(Animator animator, RuntimeAnimatorController newController)
	{
		SkeletonEdit component = animator.GetComponent<SkeletonEdit>();
		if (component)
		{
			component.Disable();
		}
		animator.runtimeAnimatorController = newController;
		if (component)
		{
			component.Enable();
		}
	}

	// Token: 0x0600090B RID: 2315 RVA: 0x00054D18 File Offset: 0x00052F18
	public void PlayAnimation(string animationName, bool poseMode = false, bool immediate = false)
	{
		RuntimeAnimatorController runtimeAnimatorController = null;
		if (animationName == null)
		{
			Debug.LogError("No animation name has been given");
			return;
		}
		if (!poseMode)
		{
			if (!AnimationManager.iomanager.animationControllers.TryGetValue(animationName, out runtimeAnimatorController))
			{
				Debug.LogError("Animation \"" + animationName + "\" not found.");
				return;
			}
		}
		else if (!IOManager.Instance.poses.Contains(animationName))
		{
			Debug.LogError("Pose \"" + animationName + "\" not found.");
			return;
		}
		this.agent.SetPoseMode(poseMode);
		this.nameAnimation = animationName;
		if (poseMode)
		{
			this.unityAnimator.Play(animationName);
			if (this.giantess)
			{
				this.giantess.InvokeColliderUpdate();
			}
		}
		else
		{
			if (this.unityAnimator.runtimeAnimatorController != runtimeAnimatorController)
			{
				Vector3 position = base.transform.position;
				AnimationManager.SetAnimatorController(this.unityAnimator, runtimeAnimatorController);
				base.transform.position = position;
			}
			this.UpdateAnimationSpeed();
			if (immediate)
			{
				this.unityAnimator.Play(animationName);
			}
			else if (Time.time < this.lastChange + this.transitionDuration)
			{
				base.Invoke("CrossFade", this.transitionDuration);
			}
			else
			{
				this.CrossFade();
			}
		}
	}

	// Token: 0x0600090C RID: 2316 RVA: 0x00054E6A File Offset: 0x0005306A
	public bool IsInPose()
	{
		return this.agent.poseMode;
	}

	// Token: 0x0600090D RID: 2317 RVA: 0x00054E77 File Offset: 0x00053077
	public bool AnimationHasFinished()
	{
		return this.GetAnimationProgress() > 1f && !this.unityAnimator.IsInTransition(0);
	}

	// Token: 0x0600090E RID: 2318 RVA: 0x00054E9C File Offset: 0x0005309C
	public float GetAnimationTime()
	{
		AnimatorStateInfo currentAnimatorStateInfo = this.unityAnimator.GetCurrentAnimatorStateInfo(0);
		return currentAnimatorStateInfo.normalizedTime * currentAnimatorStateInfo.length;
	}

	// Token: 0x0600090F RID: 2319 RVA: 0x00054EC8 File Offset: 0x000530C8
	public float GetAnimationLength()
	{
		return this.unityAnimator.GetCurrentAnimatorStateInfo(0).length;
	}

	// Token: 0x06000910 RID: 2320 RVA: 0x00054EEC File Offset: 0x000530EC
	public float GetAnimationProgress()
	{
		return this.unityAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime;
	}

	// Token: 0x06000911 RID: 2321 RVA: 0x00054F0D File Offset: 0x0005310D
	public bool TransitionEnded()
	{
		return !this.unityAnimator.IsInTransition(0);
	}

	// Token: 0x06000912 RID: 2322 RVA: 0x00054F1E File Offset: 0x0005311E
	private void CrossFade()
	{
		this.unityAnimator.CrossFade(this.nameAnimation, this.transitionDuration);
		this.lastChange = Time.time;
	}

	// Token: 0x06000913 RID: 2323 RVA: 0x00054F42 File Offset: 0x00053142
	public void SetWalkSpeed(Vector3 direction, float speed)
	{
		if (this.agent.isGiantess)
		{
			speed *= this.GetCurrentSpeed();
			direction = direction.normalized;
		}
		this.ChangeInternalSpeed(speed);
	}

	// Token: 0x06000914 RID: 2324 RVA: 0x00054F70 File Offset: 0x00053170
	public void SetNewWalkSpeed(Vector3 direction)
	{
		float num = direction.z;
		float num2 = direction.x;
		if (num < 0f)
		{
			num = 0f;
			num2 = Mathf.Sign(num2);
		}
		this.unityAnimator.SetFloat(this.forwardHash, num);
		this.unityAnimator.SetFloat(this.rightHash, num2);
	}

	// Token: 0x06000915 RID: 2325 RVA: 0x00054FCC File Offset: 0x000531CC
	public void UpdateAnimationSpeed()
	{
		float currentSpeed = this.GetCurrentSpeed();
		this.ChangeInternalSpeed(currentSpeed);
		this.agent.movement.speedModifier = currentSpeed;
	}

	// Token: 0x06000916 RID: 2326 RVA: 0x00054FF8 File Offset: 0x000531F8
	private void ChangeInternalSpeed(float speed)
	{
		this.unityAnimator.SetFloat(this.animationSpeedHash, speed);
	}

	// Token: 0x06000917 RID: 2327 RVA: 0x0005500C File Offset: 0x0005320C
	public float GetCurrentSpeed()
	{
		float num = 1f;
		if (GlobalPreferences.SlowdownWithSize.value)
		{
			float num2 = Mathf.Log10(this.agent.Height);
			num = 1f - num2 / 5f;
			num = Mathf.Clamp(num, this.minSpeed, this.maxSpeed);
		}
		return num * this.speedMultiplier * GameController.globalSpeed;
	}

	// Token: 0x06000918 RID: 2328 RVA: 0x0005506E File Offset: 0x0005326E
	public void ChangeSpeed(float newSpeed)
	{
		this.speedMultiplier = newSpeed;
		this.UpdateAnimationSpeed();
	}

	// Token: 0x04000ED7 RID: 3799
	public float transitionDuration = 0.08f;

	// Token: 0x04000ED8 RID: 3800
	public Animator unityAnimator;

	// Token: 0x04000ED9 RID: 3801
	private Humanoid agent;

	// Token: 0x04000EDA RID: 3802
	private int animationSpeedHash;

	// Token: 0x04000EDB RID: 3803
	private int forwardHash;

	// Token: 0x04000EDC RID: 3804
	private int rightHash;

	// Token: 0x04000EDD RID: 3805
	public float minSpeed = 0.1f;

	// Token: 0x04000EDE RID: 3806
	public float maxSpeed = 1f;

	// Token: 0x04000EDF RID: 3807
	private float lastChange;

	// Token: 0x04000EE1 RID: 3809
	public float speedMultiplier = 1f;

	// Token: 0x04000EE2 RID: 3810
	private static IOManager iomanager;

	// Token: 0x04000EE3 RID: 3811
	private Giantess giantess;
}
